<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/12/24
 * Time: 9:36
 */
namespace app\admin\controller;
use think\Db;
class Circulate extends  Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            if(isset($key)&&$key!="")
            {
                $map['uuu.user_username'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.order_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.order_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.order_time'] = ['between time',[$start,$end]];
            }
            //支付方式
            if(isset($order_status) && $order_status != 0){
                $map['a.order_status'] = $order_status;
            }
            //订单状态
            if(isset($order_type) && $order_type != 0){
                $map['a.order_type'] = $order_type;
            }
//            $map['a.order_del'] = 1;
            $map['a.order_type_on'] = 2;
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_order')
                ->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_order')
                ->alias ('a')
                ->join('up_u_user uuu','uuu.user_id = a.order_userid','left')
                ->page($page, $limit)
                ->where($map)
                ->field('a.*,uuu.user_username')
                ->order('a.order_id desc')
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(session('?order_id')){
            session('order_id',null);
        }
        return $this->fetch();
    }

    /*************
     * @return \think\response\Json
     * 确认发货
     */
    public function update(){
        if(request()->isAjax()){
            $order_id = input('param.id');
            //当前登录者信息
            $orderInfo = Db::table('up_order')->where('order_id', $order_id)->find();
            //确认的订单信息
            Db::startTrans();
            try{
                //修改订单状态
               Db::table('up_order')->where('order_id', $order_id)->setField('order_status', 3);
                //插入消息表
                $data = [
                    'n_title' => '商城订单完成',//标题
                    'n_content' => '您编号为' . $orderInfo['order_number'] . '的订单已确认发货',//内容
                    'n_type' => 2,//个人消息
                    'n_user_id' => $orderInfo['order_userid'],//用户ID
                    'n_read' => 0,//已读
                    'n_delete' => 0,//已删除
                    'n_time' => date('Y-m-d H:i:s')//发布时间
                ];
              Db::table('up_notice')->insert($data);
              Db::commit();
                try{
                    $user_id = (string)$data['n_user_id'];
                    $this->Jpush(1, $user_id, $data['n_content'], ['type' => '']);
                }catch(\Exception $e){
        
                }
                return  json(array('code'=>200,'msg'=>"确认发货成功"));
            }catch(\Exception $e){
                Db::rollback();
                return  json(array('code'=>100,'msg'=>"确认发货失败"));
            }
        }
    }

    public function detail(){
        if(request()->isAjax()){
            extract(input());

            $map =array();
            if(isset($key)&&$key!="")
            {
                $map['oi_good_name'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.oi_pay_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.oi_pay_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.oi_pay_time'] = ['between time',[$start,$end]];
            }
            $map['a.oi_order_id'] =input('param.id');
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_order_info')->alias('a')
                ->where($map)
                ->count();//计算总页面
            $list = Db::table('up_order_info')
                ->alias ('a')
                ->page($page, $limit)
                ->where($map)
                ->field(
                    ['a.oi_id','a.oi_price',
                        'oi_good_thumb',
                        'a.oi_num','a.oi_pay_time','a.oi_good_name'
                    ])
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        if(!session('?order_id')){
            session('order_id',input('param.id'));
        }
        return $this->fetch();
    }
}